/**
 * @author harae Feng 
 * @date 2019-10-09 10:17:40 
 * @Last Modified by: harae Feng
 * @Last Modified time: 2019-10-22 15:32:29
 * @Describe 抖音-榜单内容(制作-选择音乐)
 */


const fetch = require('../../util/fetchUtil').fetchRequest
const ResultAPI = require('../../routes/api/resultAPI')
const CatchHandle = require('../../routes/api/apicatch')


/**
 * 榜单内容(制作-选择音乐)
 * @param {Object-Map} options 传参 mc_id必须
 */
async function musicList(options){
    let url = `https://api-hl.amemv.com/aweme/v1/music/list/?version_code=${options.version_code}&pass-region=${options['pass-region']}&pass-route=${options['pass-route']}&js_sdk_version=${options.js_sdk_version}&app_name=${options.app_name}&vid=${options.vid}&app_version=${options.app_version}&device_id=${options.device_id}&channel=${options.channel}&mcc_mnc=${options.mcc_mnc}&aid=${options.aid}&screen_width=${options.screen_width}&openudid=${options.openudid}&os_api=${options.os_api}&ac=${options.ac}&os_version=${options.os_version}&device_platform=${options.device_platform}&build_number=${options.build_number}&device_type=${options.device_type}&iid=${options.iid}&idfa=${options.idfa}&mc_id=${options.mc_id}&cursor=${options.cursor || 0}`
    let res = await fetch(url, {
        headers: {
            "user-agent": require('./__userAgent.js')
        }
    })
    let resJSON = await res.json()
    return resJSON
}

async function musicListAPI(ctx){
    try {
        const parms = ctx.query || {}
        if(!parms.mc_id){
            return new ResultAPI(10000)
        }
        
        let __Options = Object.assign(parms, require('./__parms.js'))

        let resJSON = await musicList(__Options)
        if(resJSON.status_code === 0){
            return new ResultAPI(0, {}, resJSON)
        }else{
            return new ResultAPI(10003, {}, resJSON)
        }       
    } catch (error) {
        return CatchHandle(__dirname, __filename, error)
    }
}

module.exports = {
    musicListAPI: musicListAPI,
}